package com.lanou.musicbox.fragment.onlinemusic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.onlinemusic.NetworkMusicFragmentAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.custom.Events;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by 杨海宁 on 15/10/20.
 * 网络音乐_TabLayout
 */
public class NetworkMusicFragment extends BaseFragment{

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private View v;
    private ArrayList<Fragment> fragments;
    private  NetworkMusicFragmentAdapter adapter;
    String a;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_network_music,null);
        EventBus.getDefault().register(this);
        init();
        initFragment();
        return v;
    }
    //用EventButs接收
    public void onEvent(Events.FristEvent event){
         a = event.getMsg();
        if (a.equals("one")) {
            mViewPager.setCurrentItem(2);
        }

    }
    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new RecommendFragment());
        fragments.add(new RankingListFragment());

        fragments.add(new PlaylistFragment());
        fragments.add(new SingersFragment());
        adapter.setData(fragments);

    }


    private void init() {

        mTabLayout = (TabLayout) v.findViewById(R.id.network_music_tabLayout);
        mViewPager = (ViewPager) v.findViewById(R.id.network_music_viewPage);
        //如果是 fragment 的话,getChildFragmentManager()
        adapter = new NetworkMusicFragmentAdapter(getChildFragmentManager());
        //设置充满
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mViewPager.setAdapter(adapter);
        //必须在 setAdapter()后调用
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(adapter);
        //0xff0088ff 0xff 代表透明度(0-255),0088ff 代表颜色
        mTabLayout.setSelectedTabIndicatorColor(0xff607D8B);

    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        if (!getActivity().isDestroyed()){
//            for (Fragment fragment : fragments){
//                getFragmentManager()
//                        .beginTransaction()
//                        .remove(fragment)
//                        .commit();
//            }
//        }


    }
}
